package com.fita.entity.agent;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fita.entity.order.reverse.ReverseOrder;
import com.fita.entity.principal.District;
import com.fita.entity.principal.Principal;

/**
 * 代理申请
 * @author mohanlan
 *
 */
@Entity
@Table(name = "agent_apply")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class AgentApply {
    
    @Id
    @GeneratedValue
    private Long id;
    
    private String email;
    
    private String alias;
    
    private String name;

    @Column(unique = true)
    private String mobile;
    
    private String openid;
    
    /**
     * 代理人申请状态
     */
    @Enumerated(EnumType.STRING)
    private AgentApplyStatus status;
    
    /**
     * 代理类型
     */
    @Enumerated(EnumType.STRING)
    private AgentType type;
    
    /**
     * 所属分区
     */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private District district;
    
    /** 所属销售 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Principal principal;
    
    /**
     * 公司信息
     */
    private Long companyId;
    
    private String companyName;
    
    private String companyAddress;
    
    @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;

    @ManyToOne
    private Agent primaryAgent;

    // 一级代理返点率（百分之几）
    private Integer rate;

    /**
     * The default constructor
     */
    public AgentApply() {
    }

    /**
     * 根据询价单创建代理申请
     * @param reverseOrder
     * @param primaryAgent
     */
    public AgentApply(ReverseOrder reverseOrder, Agent primaryAgent){

        this.disabled = false;
        this.name = reverseOrder.getCustomName();
        this.mobile = reverseOrder.getCustomMobile();
        this.type = AgentType.PERSONAL;
        this.status = AgentApplyStatus.CREATED;
        this.createTime = new Date();
        this.primaryAgent = primaryAgent;
        this.principal = primaryAgent.getCompany().getPrincipal();
        this.district = primaryAgent.getDistrict();
        this.rate = primaryAgent.getCompany().getPrimaryAgentRate();
    }
    
    /**
     * 
     * @param agentCompany
     * @return
     */
    @Transient
    public Agent generateAgents(AgentCompany agentCompany) {
    	Agent agent = new Agent();
    	agent.setName(this.name);
    	agent.setEmail(this.email);
    	agent.setPerfix(this.alias);
    	agent.setDistrict(this.district);
    	agent.setMobile(this.mobile);
    	agent.setCompany(agentCompany);
    	agent.setOpenid(this.openid);
		return agent;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public AgentCompany generateAgentCompany() {
    	AgentCompany agentCompany = new AgentCompany();
    	agentCompany.setName(this.name);
    	agentCompany.setType(this.type);
    	agentCompany.setCompanyName(this.companyName);
    	agentCompany.setCompanyAddress(this.companyAddress);
    	agentCompany.setContactName(this.name);
    	agentCompany.setContactEmail(this.email);
    	agentCompany.setConactMobile(this.mobile);
    	agentCompany.setDistrict(this.district);
    	agentCompany.setPrincipal(this.principal);
    	agentCompany.setDisabledPrimaryAgent(true);
		return agentCompany;
    	
    }
    
    /**
     * 
     * @param agentCompany
     * @return
     */
    public AgentApply assignCompany(AgentCompany agentCompany) {
    	 this.companyName = agentCompany.getCompanyName();
    	 this.companyId = agentCompany.getId();
    	 return this;
    }
    
    /**
     * 
     * @param agentApply
     * @return
     */
    public AgentApply edit(AgentApply agentApply) {
    	this.email = StringUtils.isNotBlank(agentApply.getEmail()) ? agentApply.getEmail() : this.email;
    	this.name = StringUtils.isNotBlank(agentApply.getName()) ? agentApply.getName() : this.name;
		this.mobile = StringUtils.isNotBlank(agentApply.getMobile()) ? agentApply.getMobile() : this.mobile;
		this.companyAddress = agentApply.getCompanyAddress();
    	this.type = agentApply.getType();
    	this.companyName = StringUtils.isNotBlank(agentApply.getCompanyName()) ? agentApply.getCompanyName() : this.companyName;
    	return this;
    }
    
    /**
     * 
     * @param openid
     * @return
     */
    public AgentApply bindOpenid(String openid) {
        this.openid = openid;
        return this;
    }


    /**
     * 绑定一级代理
     * @param primaryAgent
     * @param rate
     * @return
     */
    public AgentApply bindPrimaryAgent(Agent primaryAgent, Integer rate) {

        this.primaryAgent = primaryAgent;

        this.rate = rate;

        return this;
    }

    /**
     * 
     * @return
     */
    public AgentApply reject() {
    	this.status = AgentApplyStatus.REJECT;
    	return this;
    }
    
    /**
     * 
     * @return
     */
    public AgentApply activate() {
    	this.status = AgentApplyStatus.AUTHING;
    	return this;
    }
    /**
     * 
     * @return
     */
    public AgentApply verified() {
    	this.status = AgentApplyStatus.FINISHED;
    	return this;
    }
    
    /**
     * 
     * @return
     */
    public AgentApply initliaze() {
    	
    	this.disabled = false;
    	this.createTime = new Date();
    	this.status = AgentApplyStatus.CREATED;
    	this.principal = null;
    	this.companyAddress = null;
        return this;
    }
   
    /**
     * 
     * @param district
     * @return
     */
    public AgentApply editDistrict(District district) {
    	this.district = district;
    	return this;
    }
    
    /**
     * 
     * @param district
     * @return
     */
    public AgentApply District(District district) {
    	this.district = district;
    	this.status = AgentApplyStatus.CREATED;
    	this.principal = null;
    	return this;
    }
    
    /**
     * 
     * @param principal
     * @return
     */
    public AgentApply principal(Principal principal) {
    	this.principal = principal;
    	this.status = AgentApplyStatus.AUTHING;
    	return this;
    }
    
    /**
     * 
     * @param principal
     * @return
     */
    public AgentApply editPrincipal(Principal principal) {
    	this.principal = principal;
    	return this;
    }
    
    /**
     * 
     * @return
     */
    public AgentApply frost() {
    	this.disabled = true;
    	return this;
    }
    
    /**
     * 
     * @return
     */
    public AgentApply activation() {
    	this.disabled = false;
    	return this;
    }
    
    /**
     * 
     */
    public AgentApply clearCompanyInformation() {

        this.companyAddress = null;
        
        this.companyName = null;
        
        return this;
    }
   

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the alias
     */
    public String getAlias() {
        return alias;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the openid
     */
    public String getOpenid() {
        return openid;
    }

    /**
     * @return the status
     */
    public AgentApplyStatus getStatus() {
        return status;
    }

    /**
     * @return the type
     */
    public AgentType getType() {
        return type;
    }

    /**
     * @return the district
     */
    public District getDistrict() {
        return district;
    }

    /**
     * @return the principal
     */
    public Principal getPrincipal() {
        return principal;
    }

    /**
     * @return the companyId
     */
    public Long getCompanyId() {
        return companyId;
    }

    /**
     * @return the companyName
     */
    public String getCompanyName() {
        return companyName;
    }

    /**
     * @return the companyAddress
     */
    public String getCompanyAddress() {
        return companyAddress;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @param alias the alias to set
     */
    public void setAlias(String alias) {
        this.alias = alias;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param openid the openid to set
     */
    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(AgentApplyStatus status) {
        this.status = status;
    }

    /**
     * @param type the type to set
     */
    public void setType(AgentType type) {
        this.type = type;
    }

    /**
     * @param district the district to set
     */
    public void setDistrict(District district) {
        this.district = district;
    }

    /**
     * @param principal the principal to set
     */
    public void setPrincipal(Principal principal) {
        this.principal = principal;
    }

    /**
     * @param companyId the companyId to set
     */
    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    /**
     * @param companyName the companyName to set
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    /**
     * @param companyAddress the companyAddress to set
     */
    public void setCompanyAddress(String companyAddress) {
        this.companyAddress = companyAddress;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public Agent getPrimaryAgent() {
        return primaryAgent;
    }

    public void setPrimaryAgent(Agent primaryAgent) {
        this.primaryAgent = primaryAgent;
    }

    public Integer getRate() {
        return rate;
    }

    public void setRate(Integer rate) {
        this.rate = rate;
    }

}
